/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.veditor.view.hyperlink;

import java.awt.CardLayout;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JTree;
import javax.swing.text.Element;
import javax.swing.tree.DefaultMutableTreeNode;
import offset.nodes.Constants;
import offset.nodes.client.chooser.view.QueryResultChooserDialog;
import offset.nodes.client.model.HierarchyTree;
import offset.nodes.client.model.NodeTypes;
import offset.nodes.client.model.PanelContainer;
import offset.nodes.client.model.Server;
import offset.nodes.client.model.ServerModel;
import offset.nodes.client.model.SimpleNamespaceRegistry;
import offset.nodes.client.virtual.model.jcr.QName;
import offset.nodes.client.virtual.model.jcr.nodetype.NodeTypeDefinition;
import offset.nodes.client.view.SchemaNodeRenderer;
import offset.nodes.client.view.SimpleDialog;
import offset.nodes.client.view.TreeNodeChooserDialog;
import offset.nodes.client.editor.Editor;
import offset.nodes.client.editor.model.DocumentContext;
import offset.nodes.client.veditor.model.DataModel;
import offset.nodes.client.editor.model.messages.GetUUIDForPath;
import offset.nodes.client.editor.view.hyperlink.DataHyperlinkInfo;
import offset.nodes.client.veditor.model.VirtualHyperlinkModel;
import offset.nodes.client.veditor.view.SelectSchemaDialog;

/**
 *
 * @author  Walter Lütgenau
 */
public class DataHyperlinkPanel extends javax.swing.JPanel {

    Editor editor = null;
    JTree structure = null;
    NodeTypes types = null;
    SimpleNamespaceRegistry namespaces = SimpleNamespaceRegistry.getInstance();
    PanelContainer container = null;
    Server server = null;
    String[] workflowTransitions;
    int schemaType = DataHyperlinkInfo.SCHEMA_NEW;
    public static final int INDEX_SIMPLE = 0;
    public static final int INDEX_TO_THIS = 1;
    public static final int INDEX_FROM_THIS = 2;
    protected static final String CARD_TEMPLATE_PAGE = "templatePageSelector";
    protected static final String CARD_TRANSITION = "transitionSelector";
    String contentType = Constants.CONTENT_TYPE_VIRTUAL;

    /** Creates new form DataHyperlinkPanel */
    public DataHyperlinkPanel() {
        initComponents();
    }

    protected VirtualHyperlinkModel getVirtualModel() {
        return new VirtualHyperlinkModel(new DocumentContext(editor.getBrowserPane()), editor);
    }

    public void init(Editor editor, NodeTypes types, PanelContainer container, URL serverAddress) throws Exception {
        this.editor = editor;
        this.structure = editor.getStructureTree();
        this.types = types;
        this.container = container;
        this.server = new ServerModel(serverAddress);
        this.workflowTransitions = getVirtualModel().getWorkflowTransitions();

        if (workflowTransitions == null) {
            ((CardLayout) selectorPanel.getLayout()).show(selectorPanel, CARD_TEMPLATE_PAGE);
            contentType = Constants.CONTENT_TYPE_VIRTUAL;
        }
        else {
            ((CardLayout) selectorPanel.getLayout()).show(selectorPanel, CARD_TRANSITION);
            contentType = Constants.CONTENT_TYPE_WORKFLOW;
        }

        /*
         * 1. Check, if there is a hyperlink already at the selected location. If true, check, if it is a data hyperlink. If true, again, reuse its parameters.
         * 2. If there is no data hyperlink at the selected location, check if any of the parent elements is associated to a node type. If found,
         * use it as the base node type.
         */

        DataHyperlinkInfo dataHyperlink = getDataHyperlink(editor);
        if (dataHyperlink != null) {
            nodeTypeTextfield.setText(dataHyperlink.getNodeTypePrefixName());
            if (contentType.equals(Constants.CONTENT_TYPE_VIRTUAL)) {
                templatePageTextfield.setText(dataHyperlink.getVirtualPageTemplateUUID());
                ((CardLayout) selectorPanel.getLayout()).show(selectorPanel, CARD_TEMPLATE_PAGE);
            } else {
                ((DefaultComboBoxModel) transitionsCombo.getModel()).addElement("");
                for (String transition : workflowTransitions) {
                    ((DefaultComboBoxModel) transitionsCombo.getModel()).addElement(transition);
                }
                transitionsCombo.setSelectedItem(dataHyperlink.getWorkflowTransition());
                ((CardLayout) selectorPanel.getLayout()).show(selectorPanel, CARD_TRANSITION);
            }

            namePropertyTextfield.setText(dataHyperlink.getNameProperty());
            switch (dataHyperlink.getReferenceType()) {
                case DataHyperlinkInfo.REFERENCE_TO_THIS:
                    toThisRadio.setSelected(true);
                    break;

                case DataHyperlinkInfo.REFERENCE_FROM_THIS:
                    fromThisRadio.setSelected(true);
                    break;

                case DataHyperlinkInfo.REFERENCE_SUBNODE:
                    subnodeRadio.setSelected(true);
            }

            if (dataHyperlink.isSelectNode())
                selectNodeCheckBox.setSelected(true);
            else
                selectNodeCheckBox.setSelected(false);

        } else {
            String nodeTypeName = getParentNodeType(editor);
            if (nodeTypeName != null)
                nodeTypeTextfield.setText(nodeTypeName);
            selectNodeCheckBox.setSelected(false);
            if (workflowTransitions != null) {
                ((DefaultComboBoxModel) transitionsCombo.getModel()).addElement("");
                for (String transition : workflowTransitions) {
                    ((DefaultComboBoxModel) transitionsCombo.getModel()).addElement(transition);
                }
                transitionsCombo.setSelectedIndex(0);
            }

        }

        if (nodeTypeTextfield.getText() != null && nodeTypeTextfield.getText().length() > 0) {
            enableTemplatePage(true);
            enableNameProperty(true);
            schemaType = DataHyperlinkInfo.SCHEMA_EXTENDED;
        }

        enableOkButton();
    }

    /**
     * Return the result, i.e. the values, the user has entered
     * @return the result as a DataHyperlinkInfo
     */
    public DataHyperlinkInfo getResult() {
        DataHyperlinkInfo result = new DataHyperlinkInfo();

        result.setContentType(contentType);
        result.setNodeTypePrefixName(nodeTypeTextfield.getText());
        if (workflowTransitions == null)
            result.setVirtualPageTemplateUUID(templatePageTextfield.getText());
        else
            result.setWorkflowTransition((String) transitionsCombo.getSelectedItem());
        result.setNameProperty(namePropertyTextfield.getText());

        if (toThisRadio.isSelected())
            result.setReferenceType(DataHyperlinkInfo.REFERENCE_TO_THIS);
        else if (fromThisRadio.isSelected())
            result.setReferenceType(DataHyperlinkInfo.REFERENCE_FROM_THIS);
        else
            result.setReferenceType(DataHyperlinkInfo.REFERENCE_SUBNODE);

        if (selectNodeCheckBox.isSelected())
            result.setSelectNode(true);
        else
            result.setSelectNode(false);

        return result;
    }

    protected String getParentNodeType(Editor editor) {
        VirtualHyperlinkModel model = new VirtualHyperlinkModel(new DocumentContext(editor.getBrowserPane()), editor);

        int pos = editor.getBrowserPane().getSelectionStart();
        Element e = model.getDocument().getParagraphElement(pos); // p-implied
        if (e != null && e.getElementCount() > 0)
            e = e.getElement(0); // content

        return model.getParentNodeType(e);
    }

    /**
     * Get hyperlink information from the hyperlink element in the editor.
     * 
     * @param editor The editor
     * @return the data hyperlink information
     */
    protected DataHyperlinkInfo getDataHyperlink(Editor editor) throws UnsupportedEncodingException {
        VirtualHyperlinkModel model = getVirtualModel();
        int pos = editor.getBrowserPane().getSelectionStart();
        Element e = model.getDocument().getParagraphElement(pos); // p-implied
        if (e != null && e.getElementCount() > 0)
            e = e.getElement(0); // content

        DataHyperlinkInfo result = model.getDataHyperlinkInfo(e);

        return result;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        referenceButtonGroup = new javax.swing.ButtonGroup();
        nodeTypeLabel = new javax.swing.JLabel();
        namePropertyLabel = new javax.swing.JLabel();
        nodeTypeTextfield = new javax.swing.JTextField();
        namePropertyTextfield = new javax.swing.JTextField();
        nodeTypeButton = new javax.swing.JButton();
        namePropertyButton = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        selectNodeCheckBox = new javax.swing.JCheckBox();
        toThisRadio = new javax.swing.JRadioButton();
        fromThisRadio = new javax.swing.JRadioButton();
        subnodeRadio = new javax.swing.JRadioButton();
        selectorPanel = new javax.swing.JPanel();
        templatePageSelectorPanel = new javax.swing.JPanel();
        templatePageLabel = new javax.swing.JLabel();
        templatePageButton = new javax.swing.JButton();
        templatePageTextfield = new javax.swing.JTextField();
        transitionSelectorPanel = new javax.swing.JPanel();
        transitionsCombo = new javax.swing.JComboBox();
        transitionLabel = new javax.swing.JLabel();

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("offset/nodes/client/editor/view/resources/EditorBundle"); // NOI18N
        nodeTypeLabel.setText(bundle.getString("Hyperlink.Data.nodeType")); // NOI18N

        namePropertyLabel.setText(bundle.getString("Hyperlink.Data.nameProperty")); // NOI18N
        namePropertyLabel.setEnabled(false);

        namePropertyTextfield.setEnabled(false);

        nodeTypeButton.setText("...");
        nodeTypeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectSchema(evt);
            }
        });

        namePropertyButton.setText("...");
        namePropertyButton.setEnabled(false);
        namePropertyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                setNameProperty(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("Hyperlink.reference"))); // NOI18N

        selectNodeCheckBox.setText(bundle.getString("hyperlink.selectNode")); // NOI18N
        selectNodeCheckBox.setMargin(new java.awt.Insets(2, 0, 2, 2));

        referenceButtonGroup.add(toThisRadio);
        toThisRadio.setSelected(true);
        toThisRadio.setText(bundle.getString("Hyperlink.toThis")); // NOI18N
        toThisRadio.setMargin(new java.awt.Insets(2, 0, 2, 0));
        toThisRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                toThisSelected(evt);
            }
        });

        referenceButtonGroup.add(fromThisRadio);
        fromThisRadio.setText(bundle.getString("Hyperlink.fromThis")); // NOI18N
        fromThisRadio.setMargin(new java.awt.Insets(2, 0, 2, 0));
        fromThisRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fromThisSelected(evt);
            }
        });

        referenceButtonGroup.add(subnodeRadio);
        subnodeRadio.setText(bundle.getString("Hyperlink.subnode")); // NOI18N
        subnodeRadio.setMargin(new java.awt.Insets(2, 0, 2, 0));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(109, 109, 109)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(subnodeRadio)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(toThisRadio)
                        .addGap(232, 232, 232)
                        .addComponent(selectNodeCheckBox))
                    .addComponent(fromThisRadio))
                .addGap(19, 19, 19))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(toThisRadio)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(fromThisRadio)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(subnodeRadio))
                    .addComponent(selectNodeCheckBox))
                .addContainerGap(20, Short.MAX_VALUE))
        );

        selectorPanel.setLayout(new java.awt.CardLayout());

        templatePageLabel.setText(bundle.getString("Hyperlink.Data.templatePage")); // NOI18N
        templatePageLabel.setEnabled(false);

        templatePageButton.setText("...");
        templatePageButton.setEnabled(false);
        templatePageButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectTemplatePage(evt);
            }
        });

        templatePageTextfield.setEnabled(false);

        javax.swing.GroupLayout templatePageSelectorPanelLayout = new javax.swing.GroupLayout(templatePageSelectorPanel);
        templatePageSelectorPanel.setLayout(templatePageSelectorPanelLayout);
        templatePageSelectorPanelLayout.setHorizontalGroup(
            templatePageSelectorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(templatePageSelectorPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(templatePageLabel)
                .addGap(41, 41, 41)
                .addComponent(templatePageTextfield, javax.swing.GroupLayout.DEFAULT_SIZE, 335, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(templatePageButton)
                .addGap(24, 24, 24))
        );
        templatePageSelectorPanelLayout.setVerticalGroup(
            templatePageSelectorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(templatePageSelectorPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(templatePageSelectorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(templatePageLabel)
                    .addComponent(templatePageButton)
                    .addComponent(templatePageTextfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        selectorPanel.add(templatePageSelectorPanel, "templatePageSelector");

        transitionsCombo.setModel(new DefaultComboBoxModel());

        transitionLabel.setText(bundle.getString("Hyperlink.Data.transitionsLable")); // NOI18N

        javax.swing.GroupLayout transitionSelectorPanelLayout = new javax.swing.GroupLayout(transitionSelectorPanel);
        transitionSelectorPanel.setLayout(transitionSelectorPanelLayout);
        transitionSelectorPanelLayout.setHorizontalGroup(
            transitionSelectorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(transitionSelectorPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(transitionLabel)
                .addGap(66, 66, 66)
                .addComponent(transitionsCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(205, Short.MAX_VALUE))
        );
        transitionSelectorPanelLayout.setVerticalGroup(
            transitionSelectorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(transitionSelectorPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(transitionSelectorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(transitionsCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(transitionLabel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        selectorPanel.add(transitionSelectorPanel, "transitionSelector");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(nodeTypeLabel)
                .addGap(63, 63, 63)
                .addComponent(nodeTypeTextfield, javax.swing.GroupLayout.DEFAULT_SIZE, 334, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(nodeTypeButton)
                .addGap(24, 24, 24))
            .addComponent(selectorPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 538, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(namePropertyLabel)
                        .addGap(41, 41, 41)
                        .addComponent(namePropertyTextfield, javax.swing.GroupLayout.DEFAULT_SIZE, 334, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(namePropertyButton)))
                .addGap(24, 24, 24))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nodeTypeButton)
                    .addComponent(nodeTypeTextfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(nodeTypeLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(selectorPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(namePropertyLabel)
                    .addComponent(namePropertyTextfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(namePropertyButton))
                .addGap(37, 37, 37)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(44, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    private void selectSchema(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectSchema
        DefaultMutableTreeNode structureNode = (DefaultMutableTreeNode) structure.getSelectionPath().getLastPathComponent();
        Element element = (Element) structureNode.getUserObject();

        // select schema, i.e. node type
        SelectSchemaDialog dialog = new SelectSchemaDialog(structure, types, SelectSchemaDialog.SCHEMA_EXTENDED);
        if (element.getAttributes().isDefined(DataModel.ATTRIBUTE_NAME_DATA))
            dialog.setSchemaName((String) element.getAttributes().getAttribute(DataModel.ATTRIBUTE_NAME_DATA));
        dialog.show();
        if (dialog.getReturnStatus() == SelectSchemaDialog.RET_OK) {
            nodeTypeTextfield.setText(dialog.getSchemaName());
            enableTemplatePage(true);
            enableNameProperty(true);
            schemaType = DataHyperlinkInfo.SCHEMA_EXTENDED;
        }
    }//GEN-LAST:event_selectSchema

    private void setNameProperty(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_setNameProperty
        QName typeName = QName.valueOf(namespaces.toQNameString(getType(nodeTypeTextfield.getText())));
        DefaultMutableTreeNode querySchemaNode = HierarchyTree.setupHierarchicalTree(QName.ANY_NAME,
                (NodeTypeDefinition) types.get(typeName), null, types, true, true);
        TreeNodeChooserDialog chooser = new TreeNodeChooserDialog(null, true, querySchemaNode);
        chooser.setCellRenderer(new SchemaNodeRenderer());
        chooser.setVisible(true);
        if (chooser.getReturnStatus() == TreeNodeChooserDialog.RET_OK) {
            DefaultMutableTreeNode selected = chooser.getSelectedNode();
            namePropertyTextfield.setText(selected.toString());
            enableOkButton();
        }

    }//GEN-LAST:event_setNameProperty

    private void selectTemplatePage(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectTemplatePage
        String filter = "//element(*, " + Constants.TYPE_VIRTUAL_PAGE_INSTANCE + ")[@" + Constants.PROP_NODETYPE + "='" + getType(nodeTypeTextfield.getText()) + "']";
        try {
            QueryResultChooserDialog dialog = new QueryResultChooserDialog(null, true, server, filter);
            dialog.setVisible(true);
            if (dialog.getReturnStatus() == SimpleDialog.OK) {
                String path = dialog.getPath();
                GetUUIDForPath.Request request = new GetUUIDForPath.Request();
                request.setPath(path);
                GetUUIDForPath.Response response = (GetUUIDForPath.Response) server.sendRequest(request);
                templatePageTextfield.setText(response.getUuid());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }//GEN-LAST:event_selectTemplatePage

    private void toThisSelected(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_toThisSelected
        if (toThisRadio.isSelected()) { // currently, we do not support toThis reference type and node selection
            selectNodeCheckBox.setEnabled(false);
            selectNodeCheckBox.setSelected(false);
        } else
            selectNodeCheckBox.setEnabled(true);
    }//GEN-LAST:event_toThisSelected

    private void fromThisSelected(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fromThisSelected
        // update dependant components
        toThisSelected(evt);
    }//GEN-LAST:event_fromThisSelected

    protected void enableOkButton() {
        if (namePropertyTextfield.getText() != null)
            container.enableOkButton(true);
        else
            container.enableOkButton(false);
    }

    protected void enableNameProperty(boolean enable) {
        namePropertyButton.setEnabled(enable);
        namePropertyLabel.setEnabled(enable);
        namePropertyTextfield.setEnabled(enable);
    }

    protected void enableTemplatePage(boolean enable) {
        templatePageButton.setEnabled(enable);
        templatePageLabel.setEnabled(enable);
        templatePageTextfield.setEnabled(enable);
    }

    protected String getType(String path) {
        int index = path.lastIndexOf("/");
        if (index >= 0)
            path = path.substring(index + 1);
        return path.replaceAll("\\[.*\\]", "");
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton fromThisRadio;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton namePropertyButton;
    private javax.swing.JLabel namePropertyLabel;
    private javax.swing.JTextField namePropertyTextfield;
    private javax.swing.JButton nodeTypeButton;
    private javax.swing.JLabel nodeTypeLabel;
    private javax.swing.JTextField nodeTypeTextfield;
    private javax.swing.ButtonGroup referenceButtonGroup;
    private javax.swing.JCheckBox selectNodeCheckBox;
    private javax.swing.JPanel selectorPanel;
    private javax.swing.JRadioButton subnodeRadio;
    private javax.swing.JButton templatePageButton;
    private javax.swing.JLabel templatePageLabel;
    private javax.swing.JPanel templatePageSelectorPanel;
    private javax.swing.JTextField templatePageTextfield;
    private javax.swing.JRadioButton toThisRadio;
    private javax.swing.JLabel transitionLabel;
    private javax.swing.JPanel transitionSelectorPanel;
    private javax.swing.JComboBox transitionsCombo;
    // End of variables declaration//GEN-END:variables
}
